﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NCalc;
using Crom.Controls;

namespace Unreal_Unit_Converter_2
{
    public enum Units
    {
        UNREAL_UNITS,
        MAX_UNITS,
        METERS
    }
    public partial class UUConverter : Form
    {
        public Label[] lUUGridArray;
        public Label[] lMaxGridArray;
        public UUConverter()
        {
            InitializeComponent();
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (sender.GetType() == typeof(TextBox)) ((TextBox)sender).Text = UnrealConverter.CalculateMathExpression(((TextBox)sender).Text);
            }
        }

        private void UUConverter_Load(object sender, EventArgs e)
        {
            DockContainer dc = new DockContainer();
            this.Controls.Add(dc);
            lUUGridArray = new Label[6];
            lMaxGridArray = new Label[6];
        }
    }

    public static class UnrealConverter
    {
        private static double fUUtoMax;
        private static double fUUtoMeters;
        private static double fMaxtoUU;
        private static double fMaxtoMeters;
        private static double fMeterstoUU;
        private static double fMeterstoMax;
        private static DataGridView dgvDataGridView;
        private static Label[] lUUGridArray;
        private static Label[] lMaxGridArray;
        private static Label lMaxtoUUPercentageIncrease;
        public static void SetReferences(DataGridView dgv, Label[] lUUGridArray_ref, Label[] lMaxGridArray_ref, Label lMaxtoUUPercentageIncrease_ref)
        {
            dgvDataGridView = dgv;
            lUUGridArray = lUUGridArray_ref;
            lMaxGridArray = lMaxGridArray_ref;
            lMaxtoUUPercentageIncrease = lMaxtoUUPercentageIncrease_ref;
        }
        public static void DataGridViewCellTextBox_TextChangedEventHandler(object sender, EventArgs e)
        {
            DataGridViewCell dgvcUUInput = dgvDataGridView[0, 0];
            DataGridViewCell dgvcMaxInput = dgvDataGridView[1, 0];
            DataGridViewCell dgvcMetersInput = dgvDataGridView[2, 0];
            GetCreateRatios((string)dgvcUUInput.Value, (string)dgvcMaxInput.Value, (string)dgvcMetersInput.Value);
        }
        public static void GetCreateRatios(string strUU, string strMax, string strMeters)
        {
            double UU, Max, Meters;

            strUU = UnrealConverter.CalculateMathExpression(strUU);
            strMax = UnrealConverter.CalculateMathExpression(strMax);
            strMeters = UnrealConverter.CalculateMathExpression(strMeters);

            Double.TryParse(strUU, out UU);
            Double.TryParse(strMax, out Max);
            Double.TryParse(strMeters, out Meters);

            fUUtoMax = Max / UU;
            fUUtoMeters = Meters / UU;
            fMaxtoUU = 1 / fUUtoMax;
            fMaxtoMeters = Meters / Max;
            fMeterstoUU = 1 / fUUtoMeters;
            fMeterstoMax = 1 / fMaxtoMeters;
        }
        public static void Update(Units From, DataGridView dgv, Label[] lUUGridArray, Label[] lMaxGridArray, Label lMaxtoUUPercentageIncrease)
        {
        }
        public static bool isValid(string strInput, out double fOutput)
        {
            return Double.TryParse(strInput, out fOutput);
        }
        public static void UpdateGridArrays(Label[] lGridArray, ref double value)
        {
            int divisor = 4;
            double fResult;
            foreach (Label l in lGridArray)
            {
                l.Text = "";
                l.Visible = true;
                fResult = value / divisor;
                bool cleanDivide = (value % divisor) == 0 ? true : false;
                int iResult = (int)fResult;
                if (iResult > 0 && cleanDivide)
                {
                    l.Text = iResult.ToString();
                    l.ForeColor = Color.SteelBlue;
                }
                divisor *= 2;
            }
        }
        public static string CalculateMathExpression(string input)
        {
            Expression e = new Expression(input);
            return e.Evaluate().ToString();   
        }
    }
}
